import { execSync } from 'child_process';

const PORT = 8080;

try {
    // 查找占用端口的进程
    const findCommand = `netstat -ano | findstr :${PORT}`;
    const output = execSync(findCommand, { encoding: 'utf-8' });

    // 提取所有PID
    const pids = new Set();
    output.split('\n').forEach(line => {
        const match = line.match(/(\d+)$/);
        if (match) {
            pids.add(match[1]);
        }
    });

    // 终止所有找到的进程
    pids.forEach(pid => {
        try {
            execSync(`taskkill /F /PID ${pid}`, { encoding: 'utf-8' });
            console.log(`Successfully killed process ${pid}`);
        } catch (err) {
            console.log(`Failed to kill process ${pid}: ${err.message}`);
        }
    });
} catch (err) {
    // 如果没有找到进程，也是正常的
    if (!err.message.includes('没有找到')) {
        console.error(`Error: ${err.message}`);
    }
} 